import webSocket from '@ohos.net.webSocket';
var _request = Symbol();
var _url = Symbol();
class WebSocket {
    constructor() {
      this[_request] = webSocket.createWebSocket();
    }
    get socketObject(){
     return this[_request];
    }
   
    url(value){
        this[_url] = value;
        return this;
    }
    connect(success,error){
        
        let promise =  this.socketObject.connect(this[_url]);
        promise.then(() => {
            success("Connected")
        }).catch((err) => {
            error(err)
        });
        
    }

    sendMessage(message,success,error){
        if(this.socketObject==null ||  this.socketObject==undefined){
            error("Not Connected")
        }
       
        let promise = this.socketObject.send(message);
        promise.then((value) => {
           success(value)
        }).catch((err) => {
            error(err)
        });

        this.socketObject.on('message', (err, value) => {
            if(value){
                success(value)
            }else{
                error(err)
            }
        });
        return this;
    }

    webSocketDisconnect(success,error){
        if(this.socketObject==null ||  this.socketObject==undefined){
            error("Not Connected")
        }
            let promise = this.socketObject.close();
            promise.then(() => {
                this[_request]=null;
                success("close success")
                
            }).catch((err) => {
                error("close fail, err is " + JSON.stringify(err))
            });
        
    }

  
}
export default WebSocket;